<?php
class tools extends top
{
	//重定义构造函数
    function __construct(){  
        parent::__construct();
        if($_SESSION['POWER']!=1 && $_SESSION['POWER']!=3){$this->jump(spUrl('main', 'index'));}
        //定义当前导航
        $this->nav_name = 'tools';
        $this->page_title = '工具';
        $this->breadcrumb += array('1'=>array('name'=>'工具'));
    }
    //构造左侧导航
    function __left_nav_common(){
		$left_nav_result = array(
				array(
						'id'=>1,
						'name'=>'网站备份',
						'url'=>spUrl('tools','webback'),
					),
				array(
						'id'=>2,
						'name'=>'链接更新',
						'url'=>spUrl('tools','urlupdate'),
					),
			);
		$this->left_nav_result = $left_nav_result; 	
    }
	function index(){
		$this->webback();
		$this->display("tpl/tools_webback.html");
	}	
	function webback(){
		$data = $this->spArgs();
		$this->__left_nav_common();
		$this->left_nav_id = 1;
		$this->breadcrumb += array('2'=>array('name'=>'网站备份'));
		//数据库处理
		$db = spClass('dbbackup',array($GLOBALS['G_SP']['db']));
		$this->table = $db->showAllTable($this->spArgs('chk'));
		$this->chk = $this->spArgs('chk');
		
		$this->curr_database = ' id="current"';
	}
	function dbbackup(){
		$_SESSION['PASS'] = md5(md5($this->spArgs('password').'ytcms').'check_session');
		if(!$this->check_session()){$this->jump(spUrl('main', 'index'));}
		//数据库处理
		$db = spClass('dbbackup',array($GLOBALS['G_SP']['db']));
		if($this->spArgs('dbac') == 'op') { $db->optimizeTable($this->spArgs('tabl'));exit;}
		if($this->spArgs('dbac') == 'rep') { $db->repairTable($this->spArgs('tabl'));exit;}
		if($this->spArgs('outab')) { $db->outTable($this->spArgs('outab'));exit;}
		if($this->spArgs('ouall')) { $db->outAllData();exit;}
	}
	function filebackup(){
		$_SESSION['PASS'] = md5(md5($this->spArgs('password').'ytcms').'check_session');
		if(!$this->check_session()){$this->jump(spUrl('main', 'index'));}
		echo "备份中。。。";
		$today = date("Ymd");
		mkdir ("/var/www/html/backup/$today");
		system ("tar czfP /var/www/html/backup/`date +%Y%m%d`/thinks-backup.tar.gz  /var/www/html/");
		echo "备份完成！<a href='./backup/`date +%Y%m%d`/thinks-backup.tar.gz'>下载</a>";
	}
	function urlupdate(){
		$data = $this->spArgs();
		$this->__left_nav_common();
		$this->left_nav_id = 2;
		$this->breadcrumb += array('2'=>array('name'=>'链接更新'));
		//图片、文件地址替换
		$article_url = spClass("m_url");
		if($data['dir_str_submit']&&$data['dir_str_fr']){
			$article = spClass("m_article");
			$page = spClass("m_page");
			
			$sql=	"UPDATE yt_url SET img_dir=REPLACE(img_dir, '".$data['dir_str_fr']."', '".$data['dir_str_to']."');";
			$success = '';$danger='';
			
			if($article_url->runSql($sql)){
				$success .= "链接图片地址更新成功！<br />";
			}else{
				$danger .= "链接图片地址更新出错！<br />";
			}
			
			$sql=	"UPDATE yt_article SET img_dir=REPLACE(img_dir, '".$data['dir_str_fr']."', '".$data['dir_str_to']."');";
			if($article->runSql($sql)){
				$success .= "文章封面图片地址更新成功！<br />";
			}else{
				$danger .= "文章封面图片地址更新出错！<br />";
			}
			
			$sql=	"UPDATE yt_article SET content=REPLACE(content, '".$data['dir_str_fr']."', '".$data['dir_str_to']."');";
			if($article->runSql($sql)){
				$success .= "文章内容图片地址更新成功！<br />";
			}else{
				$danger .= "文章内容图片地址更新出错！<br />";
			}
			
			$sql=	"UPDATE yt_page SET content=REPLACE(content, '".$data['dir_str_fr']."', '".$data['dir_str_to']."');";
			if($page->runSql($sql)){
				$success .= "专题内容图片地址更新成功！<br />";
			}else{
				$danger .= "专题内容图片地址更新出错！<br />";
			}
			if(!$danger){
				$this->notice = array('success',$success."全部更新成功！");
			}else{
				$this->notice = array('danger',$danger."更新出错,请谨慎处理！");
			}
			
		}
		//链接地址更新
		if($data['link_str_submit']&&$data['link_str_fr']){
			$sql=	"UPDATE yt_url SET content=REPLACE(content, '".$data['link_str_fr']."', '".$data['link_str_to']."');";
			
			if($article_url->runSql($sql)){
				$success = "链接地址更新成功！";
				$this->notice = array('success',$success);
			}else{
				$danger = "链接地址更新出错,请谨慎处理！";
				$this->notice = array('danger',$danger);
			}
		}
		//查找现有地址形式
		$dir_style_now = $article_url->findAll('img_dir!="" AND content!=""','','img_dir,content',3);
		$this->dir_style_now = array($dir_style_now[0]['img_dir'],$dir_style_now[1]['img_dir'],$dir_style_now[2]['img_dir']);
		$this->link_style_now = array($dir_style_now[0]['content'],$dir_style_now[1]['content'],$dir_style_now[2]['content']);		
	}
}